package com.loongtech.bi.manager.pay;

import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 付费分析 —付费习惯manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class HabitPayManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ActiveUserFirstAddcashPaySectionManager activeUserFirstAddcashPaySectionManager;
	@Autowired
	private ActiveUser5LevelFirstAddcashManager activeUser5LevelFirstAddcashManager;
	@Autowired
	private ActiveUserFirstAddcashDaySectionManager activeUserFirstAddcashDaySectionManager;
	
	// 查询首付金额数据 TODO
	public List<FirstPayCount> getFirstPayCountData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<Object[]> list = activeUserFirstAddcashPaySectionManager.getFirstPayUserByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		List<FirstPayCount> result = new ArrayList<FirstPayCount>();
		
		for(Object []obj: list) {
			String payCountStr = String.valueOf(obj[0]);
			int count = NumberUtil.parseInt(obj[1]);
			
			FirstPayCount fpc = new FirstPayCount();
			fpc.setPayCountStr(payCountStr);
			fpc.setCount(count);
			//
			result.add(fpc);
		}
		
		return result;
	}
	
	// 查询首付等级数据
	public List<FirstPayLevel> getFirstPayLevelData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<FirstPayLevel> result = new ArrayList<FirstPayLevel>();
		
		List<Object[]> list = activeUser5LevelFirstAddcashManager.get5LevelFirstAddcashDataByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		for(Object []obj: list) {
			int userNumber = NumberUtil.parseInt(obj[0]);
			long sumCashAdd = NumberUtil.parseLong(obj[1]);
            String levleName = obj[2].toString();
			
			FirstPayLevel fpl = new FirstPayLevel();
			fpl.setLevelStr(levleName);
			fpl.setUserCount(userNumber);
			fpl.setPayCount(sumCashAdd);
			
			result.add(fpl);
		}
		
		return result;
	}
	
	public List<FirstPayPlayTime> getFirstPayPlayTimeData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<FirstPayPlayTime> result = new ArrayList<>();
		List<Object[]> list = activeUserFirstAddcashDaySectionManager.getFirstAddcashDayByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		for(Object []obj: list) {
			String rangeStr = String.valueOf(obj[0]);
			int userNumber = NumberUtil.parseInt(obj[1]);
			
			FirstPayPlayTime fpy = new FirstPayPlayTime();
			fpy.setRangeStr(rangeStr);
			fpy.setUserNumber(userNumber);
			
			result.add(fpy);
		}
		
		return result;
	}
	
	// 首付金额数据类，Chart类
	public class FirstPayCount {
		@ChartFlow(name = "金额区间")
		private String payCountStr;
		@ChartSelect(name = "金额", key = 1)
		@ChartData1(name = "金额", key = 1)
		private int count; // 金额

		public String getPayCountStr() {
			return payCountStr;
		}

		public void setPayCountStr(String payCountStr) {
			this.payCountStr = payCountStr;
		}

		public int getCount() {
			return count;
		}

		public void setCount(int count) {
			this.count = count;
		}
	}
	
	// 首付等级数据类，Chart类
	public class FirstPayLevel {
		@ChartFlow(name = "等级")
		private String levelStr;  // 等级分组
		@ChartSelect(name = "人数", key = 1)
		@ChartData1(name = "人数", key = 1)
		private int userCount; // 人数
		@ChartSelect(name = "金额", key = 2)
		@ChartData1(name = "金额", key = 2)
		private long payCount;  // 金额

        // 根据日期排序的比较器
        public final Comparator<FirstPayLevel> comparatorLevel = new Comparator<FirstPayLevel>() {
            public int compare(FirstPayLevel s1, FirstPayLevel s2) {
                return Integer.valueOf(Integer.parseInt(s2.getLevelStr().split("-")[0])).compareTo(Integer.valueOf(Integer.parseInt(s1.getLevelStr().split("-")[0])));
            }
        };

		public String getLevelStr() {
			return levelStr;
		}

		public void setLevelStr(String levelStr) {
			this.levelStr = levelStr;
		}

		public int getUserCount() {
			return userCount;
		}

		public void setUserCount(int userCount) {
			this.userCount = userCount;
		}

		public long getPayCount() {
			return payCount;
		}

		public void setPayCount(long payCount) {
			this.payCount = payCount;
		}
	}
	
	// 首付在线时长数据类，Chart类
	public class FirstPayPlayTime {
		@ChartFlow(name = "区间")
		private String rangeStr;  // 可能是天，也可能是累计时长
		@ChartSelect(name = "游戏时长", key = 1)
		@ChartData1(name = "游戏时长", key = 1)
		private int userNumber; // 

		public String getRangeStr() {
			return rangeStr;
		}

		public void setRangeStr(String rangeStr) {
			this.rangeStr = rangeStr;
		}
		
		public int getUserNumber() {
			return userNumber;
		}

		public void setUserNumber(int userNumber) {
			this.userNumber = userNumber;
		}
	}
}
